package com.augurit.smartform.factory;

import java.util.Iterator;
import java.util.Map;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleBindings;

public class ScriptEngineFactory {
	/**
	 * 得到一个SCRIPTENGINE
	 * @return
	 */
	public static ScriptEngine getScriptEngine(){
		ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        return engine;
	}
	
	/**
	 * 得到一个绑定了变量的SCRIPTENGINE
	 * @param map
	 * @return
	 * cuigeqiang
	 */
	public static ScriptEngine getBindingScriptEngine(Map<String,Object> map){
		ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        Bindings vars = new SimpleBindings();
        Iterator it=map.entrySet().iterator();
        while(it.hasNext()){
        	Map.Entry entry=(Map.Entry)it.next();
        	String key=(String)entry.getKey();
        	Object value=entry.getValue();
        	vars.put(key, value);
        }
        engine.setBindings(vars, ScriptContext.ENGINE_SCOPE);
        return engine;
	}
}
